package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.Random;

public class Boss extends AbstractMapleStoryObject {
    public static Image[] images = new Image[200];

    static {
        /**
         * boss左面站着的
         */
        for (int i = 0; i < 8; i++) {
            images[i] = ImageUtil.getImage("boss_left_stand0" + i);
        }

        /**
         * boss向左走
         */
        for (int i = 8; i < 16; i++) {
            images[i] = ImageUtil.getImage("boss_left_walk0" + (i - 8));
        }
        /**
         * boss左面攻击
         */
        for (int i = 16; i < 36; i++) {
            images[i] = ImageUtil.getImage("boss_left_attack" + (i - 16));
        }
        /**
         * boss左面死
         */
        for (int i = 36; i < 65; i++) {
            images[i] = ImageUtil.getImage("boss_left_die" + (i - 36));
        }
        /**
         * boss右面站着
         */
        for (int i = 65; i < 73; i++) {
            images[i] = ImageUtil.getImage("boss_right_stand0" + (i - 65));
        }
        /**
         * boss右面走
         */
        for (int i = 73; i < 81; i++) {
            images[i] = ImageUtil.getImage("boss_right_walk0" + (i - 73));
        }
        /**
         * boss右面攻击
         */
        for (int i = 81; i < 101; i++) {
            images[i] = ImageUtil.getImage("boss_right_attack" + (i - 81));
        }
        /**
         * boss右面死
         */
        for (int i = 101; i < 130; i++) {
            images[i] = ImageUtil.getImage("boss_right_die" + (i - 101));
        }

        /**
         * boss的技能
         */


    }

    private int Mp;
    public int HP;
    private int MAX_MP = 100000;
    private int MAX_HP = 100000;
    private boolean shoot;
    private boolean right;
    private boolean left;


    public Boss(MapleStoryClient msc, int x, int y) {
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.live = true;
        this.action = Action.WALK;
        this.dir = Direction.RIGHT;
        this.shoot = false;


    }

    public Boss() {
        this.speed = 5;
        this.Mp = MAX_MP;
        this.HP = MAX_HP;
    }

    /**
     * 射击的方法
     */
    private int shootCount = 0;


    private int count;
    private int step;
    private int dieStep;


    @Override
    public void draw(Graphics g) {
        /**
         * 画出英雄的血条和蓝条
         */
        Color color = g.getColor();
        g.drawRect(100, 50, 1700, 30);
        g.setColor(Color.red);
        g.fillRect(100, 50, (int) (1700 * this.HP / MAX_HP), 30);
        g.setColor(color);


        if (dieStep >= 100) {
            this.live = false;
            this.action = Action.DIE;
            dieStep = 0;
        }

        if (!live) {
            msc.bosses.remove(this);
            return;
        }
        switch (dir) {
            case RIGHT:
                switch (action) {
                    case STAND:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 8 + 65], x, y, null);
                        } else {
                            g.drawImage(images[step % 8 + 65], x, y, null);
                        }
                        break;
                    case WALK:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 8 + 73], x, y, null);
                        } else {
                            g.drawImage(images[step % 8 + 73], x, y, null);
                        }
                        break;
                    case JUMP:
                        break;
                    case SHOOT:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 20 + 81], x + 250, y + 80, null);
                        } else {
                            g.drawImage(images[step % 20 + 81], x + 250, y + 80, null);
                        }
                        break;
                    case DIE:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[dieStep++ % 28 + 101], x, y, null);
                        } else {
                            g.drawImage(images[dieStep % 28 + 101], x, y, null);
                        }
                        break;
                    default:
                        break;
                }
                break;
            case LEFT:
                switch (action) {
                    case STAND:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 8], x, y, null);
                        } else {
                            g.drawImage(images[step % 8], x, y, null);
                        }

                        break;
                    case WALK:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 8 + 8], x, y, null);
                        } else {
                            g.drawImage(images[step % 8 + 8], x, y, null);
                        }
                        break;
                    case SHOOT:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 20 + 16], x - 250, y - 80, null);
                        } else {
                            g.drawImage(images[step % 20 + 16], x - 250, y - 80, null);
                        }
                        break;
                    case JUMP:
                        break;
                    case DIE:
                        if (count++ % 5 == 0) {
                            g.drawImage(images[step++ % 28 + 36], x, y, null);
                        } else {
                            g.drawImage(images[step % 28 + 36], x, y, null);
                        }
                        break;
                    default:
                        break;
                }

                break;
            default:

                break;
        }
//        confirm();
        outOfBound();
        move();

    }

    public void outOfBound() {
        if (this.dir == Direction.LEFT) {
            if (this.x <= 0) {
                this.dir = Direction.RIGHT;
            }
            if (this.x == 1500) {
                shoot = true;
            }
        }
        if (this.dir == Direction.RIGHT) {
            if (this.x >= Constant.GAME_WINDOW_WIDTH - this.width - 200) {
                this.dir = Direction.LEFT;
            }
            if (this.x == 300) {
                shoot = true;
            }
        }
        if (msc.hero.x >= this.x && msc.hero.x <= this.x + 200 ){
            this.action = Action.SHOOT;
            shoot();
        }

    }

    private int bossCount = 0;
    @Override
    public void move() {
        switch (dir) {
            case LEFT:
                switch (action) {
                    case WALK:
                        this.x -= speed;
                        break;
                    case JUMP:
//                        jump(msc.grounds);
                        break;
                    default:
                        break;
                }
                break;
            case RIGHT:
                switch (action) {
                    case WALK:
                        this.x += speed;
                        break;
                    case JUMP:
//                        jump(msc.grounds);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }

    }



    private void shoot() {
        shootCount++;
        if (shootCount % 70 == 0) {
            shoot = false;
            this.Mp -= 100;
            BossArrow bossArrow;
            if (this.dir == Direction.RIGHT) {
                bossArrow = new BossArrow(msc, x + this.width - 100, y + this.height / 2 + 150, dir);
            } else {
                bossArrow = new BossArrow(msc, x - 100, y + this.height / 2 + 150, dir);
            }
            msc.bossArrows.add(bossArrow);
            this.action = Action.STAND;
        }
    }

}
